package com.gitee.chenyaohua.oa.config;

import com.gitee.chenyaohua.oa.util.secret.SecretUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;

import javax.persistence.AttributeConverter;

@Slf4j
public class SensitiveData implements AttributeConverter<String,String> {


    @Override
    public String convertToDatabaseColumn(String attribute) {
        try {
            if(StringUtils.isEmpty(attribute)){
                return attribute;
            }
            return SecretUtils.des3Encode(attribute);
        } catch (Exception e) {
            log.error("敏感数据加密失败",e);
        }
        return null;
    }

    @Override
    public String convertToEntityAttribute(String dbData) {
        try {
            if(StringUtils.isEmpty(dbData)){
                return dbData;
            }
            return SecretUtils.des3Decode(dbData);
        } catch (Exception e) {
            log.error("敏感数据解密失败",e);
        }
        return null;
    }
}
